package Service.servicelmpl;

import Dao.DaoStuHandle;
import Model.Student;
import Service.StudentService;
import UserView.UseUi;

import java.util.Scanner;

public class StudentServicelmpl implements StudentService {
    @Override
    public void showStu(Student[] stus, int len) {
        DaoStuHandle daoStuHandle =new DaoStuHandle();
        daoStuHandle.showStuDAO(stus,len);
    }

    @Override
    public void addStu(Student[] stus, Student stu) {
        DaoStuHandle daoStuHandle = new DaoStuHandle();
        daoStuHandle.addStuDAO(stus,stu);
    }

    @Override
    public Boolean delStu(Student[] stus,Integer len, Integer id) {
        int w =0;
        boolean flag = false;
        for (int i = 0; i < len; i++) {
            if (stus[i].getId() == id){
                w = i;
                flag = true;
                break;
            }
        }

        if (flag){
            System.out.println("你确定要删除吗？ y or n");
            Scanner scanner = new Scanner(System.in);
            String  next = scanner.next();
            if (next.equals("y")) {
                DaoStuHandle daoStuHandle = new DaoStuHandle();
                daoStuHandle.delStuDAO(stus, len, w);
                return true;
            }else {
                return null;
            }
        }
        return false;
    }

    @Override
    public boolean updateStu(Student[] stus, int len, Integer id) {
        int w =0;
        boolean flag =false;
        for (int i = 0; i < len; i++) {
            if (stus[i].getId()==id){
                w=i;
                flag=true;
                break;
            }
        }
        if (flag){
            DaoStuHandle daoStuHandle = new DaoStuHandle();
            daoStuHandle.updateStuDAO(stus,w);
        }
        return flag;
    }

    public Student findById(Student[] stus, int len) {
        System.out.println("请输入要查询的学号");
        Scanner scanner = new Scanner(System.in);
        int xuehao = scanner.nextInt();
        for (int i = 0; i < len; i++) {
            if (stus[i].getId() == xuehao) {
                return stus[i];
            }
        }
        return null;
    }

    public void findByName(Student[] stus, int len, Student[] findstuName) {
        System.out.println("请输入要查询的姓名");
        Scanner scanner = new Scanner(System.in);
        String xm = scanner.next();
        for (int i = 0; i < len; i++) {
            if (stus[i].getName().equals(xm)) {
                findstuName[UseUi.num1++] = stus[i];
            }
        }
    }
}
